package com.skwyb.form.controller.admin;

import com.skwyb.form.bean.common.PageData;
import com.skwyb.form.bean.dto.SysMenu;
import com.skwyb.form.bean.req.IdReq;
import com.skwyb.form.bean.req.admin.GetMenuListReq;
import com.skwyb.form.bean.req.admin.SaveMenuReq;
import com.skwyb.form.core.BusinessException;
import com.skwyb.form.core.ObjectResult;
import com.skwyb.form.service.admin.SysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/admin/sysMenu")
@RequiredArgsConstructor
public class SysMenuController {

    private final SysMenuService menuService;

    @GetMapping("/list")
    public PageData<SysMenu> listType(@Valid GetMenuListReq req) {
        return menuService.selectMenuByPage(req);
    }


    @PostMapping("/listAll")
    public ObjectResult listAll(@Valid GetMenuListReq req) {
        return new ObjectResult(menuService.selectMenuList(req));
    }


    @PostMapping("/get")
    public ObjectResult get(@Valid IdReq req) {
        return new ObjectResult(menuService.getById(req.getId()));
    }

    @PostMapping("/save")
    public ObjectResult save(@Valid SaveMenuReq req) {
        SysMenu record = new SysMenu();
        BeanUtils.copyProperties(req, record);
        return new ObjectResult(menuService.save(record));
    }

    @PostMapping("/delete")
    public ObjectResult delete(@Valid IdReq req) throws BusinessException {
        return new ObjectResult(menuService.removeById(req.getId()));
    }
}
